package at.ac.lbg.media.vis.client.theme.render
{
	import at.ac.lbg.media.vis.client.theme.manager.PetalLibrary;
	import at.ac.lbg.media.vis.client.theme.view.Artwork;
	import at.ac.lbg.media.vis.client.theme.view.Category;
	
	import flare.util.Colors;
	import flare.vis.data.DataSprite;
	
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	import mx.core.Application;

	public class FlowerRenderer implements IArtworkRenderer
	{
		private var colorActive:uint = Colors.rgba(145, 80, 0);
		// inactive petals on active icon
		private var colorActiveMedium:uint = Colors.rgba(230, 130, 0);
		// active petal
		private var colorActiveBright:uint = Colors.rgba(255, 180, 80);

		private var colorInactive:uint = Colors.rgba(80, 70, 142);
		private var colorInactiveMedium:uint = Colors.rgba(120, 110, 180);
		private var colorInactiveBright:uint = Colors.rgba(160, 180, 216);
		
		private var colorGolden:uint = Colors.rgba(128, 111, 0);
		private var colorGoldenMedium:uint = Colors.rgba(200, 180, 0);
		private var colorGoldenBright:uint = Colors.rgba(255, 231, 0);
		
		private var colorGoldenActive:uint = Colors.rgba(153, 107, 0);
		// inactive petals on active nica icon
		private var colorGoldenActiveMedium:uint = Colors.rgba(230, 180, 0);
		// active petal on nica icon
		private var colorGoldenActiveBright:uint = Colors.rgba(255, 215, 0);
		
		public var petalLib:PetalLibrary;
		private var catDict:Dictionary;
		private var white:uint = Colors.rgba(254, 254, 254);
		
		public function FlowerRenderer()
		{
			petalLib = new PetalLibrary(colorActive, 
										colorActiveMedium,
										colorActiveBright, 
										colorInactive, 
										colorInactiveBright, 
										colorInactiveMedium,
										colorGolden,
										colorGoldenMedium,
										colorGoldenBright,
										colorGoldenActive,
										colorGoldenActiveMedium,
										colorGoldenActiveBright);
		}
		
		public function setDefaultSize(size:Number):void
		{
		}
		
		public function render(d:DataSprite):void {
			var artwork:Artwork = d as Artwork;
			catDict = Application.application.catDict;
			var x:int = 0;
			var y:int = 0;
			var centerPoint:Point = new Point(x, y);
			colorActive = Colors.rgba(230, 130, 30);
			colorInactive = Colors.rgba(150, 100, 100);
			
			
			var g:Graphics = artwork.graphics;
			g.clear();
			
			
			petalLib.initFlower(centerPoint, artwork.highlightMe > -1, artwork.year, artwork.nica);
			
			// stackCount is computed on the server and indicates the amount
			// of artworks on a stack. we do only draw a stack once and skip all
			// the other artworks on this stack.
			if (artwork.stack) {
				x = artwork.stackCount;
				y = - artwork.stackCount;
				renderStack(artwork);
				petalLib.initFlower(centerPoint, artwork.highlightMe > -1, artwork.year, artwork.nica);
				// nur fuers plakat
				//Application.application.stackstexts.data += "\n\n" + artwork.stack.label;
			} else if ((artwork.stackCount > 1) && (Application.application.useHexboard == false)) {
				return;
			}
			
			renderPetals(artwork);
		}
		
		private function renderPetals(artwork:Artwork):void {
			var i:int = 0
			
			for each (var catId:int in artwork.categoryIds) {
				if (i >= 6) {
					break;
				}
				var petal:Shape = new Shape();
				
				var cat:Category = catDict[catId] as Category;
				if (cat == null) {
					continue;
				}
				// TODO
				var index:int = cat.iconIndex;
				if (index >= petalLib.petals.length) {
				index = 1;
				}
				var active:Boolean = artwork.highlightMe == catId;
				
				petalLib.initPetal(petal, active);
				var render:Function = petalLib.petals.getItemAt(index) as Function;
				render(petal.graphics);
//				render(petalLib.petals.getItemAt(22));
				petalLib.finishPetal(petal);
				petal.rotation = 60 * i;
				artwork.addChild(petal);
				i++;
			}
			
			// add empty petal shape if artwork has less than 6 cats
			if (i < 6) {
				for (i; i < 6; i++) {
					var petal2:Shape = new Shape();
					petalLib.initPetal(petal2, false);
					var render2:Function = petalLib.petals.getItemAt(0) as Function;
					render2(petal2.graphics);
					petalLib.finishPetal(petal2);
					petal2.rotation = 60 * i;
					artwork.addChild(petal2);
				}
			}
			
		}
		
		private function renderStack(artwork:Artwork):void {
			var i:int = 0
			for each (var stackAw:Artwork in artwork.stack.artworks) {
				renderStackedPetals(artwork, stackAw, i+1);
				i++;
			}
		}
		
		private function renderStackedPetals(origAw:Artwork, artwork:Artwork, offset:int):void {
			// turn realistic stack colors-by-year off
			artwork = origAw;
			
			var i:int = 0
			petalLib.initFlowerStack(artwork.year, artwork.nica);
			
			for each (var catId:int in artwork.categoryIds) {
				if (i >= 6) {
					break;
				}
				var petal:Shape = new Shape();
				petal.y = offset;
				
				var cat:Category = catDict[catId] as Category;
				
				if (cat == null) {
					continue;
				}
				
				var index:int = cat.iconIndex;
				if (index >= petalLib.petals.length) {
					index = 1;
				}
				
				petalLib.initStackedPetal(petal, false);
				var render:Function = petalLib.petals.getItemAt(index) as Function;
				render(petal.graphics);
				petalLib.finishPetal(petal);

				petal.rotation = 60 * i;
				origAw.addChild(petal);
				i++;
			}
			
			// add empty petal shape if artwork has less than 6 cats
			if (i < 6) {
				for (i; i < 6; i++) {
					var petal2:Shape = new Shape();
					petal2.y = offset;
					petalLib.initStackedPetal(petal2, false);
					var render2:Function = petalLib.petals.getItemAt(0) as Function;
					render2(petal2.graphics);
					petalLib.finishPetal(petal2);
					petal2.rotation = 60 * i;
					origAw.addChild(petal2);
				}
			}
			
		}
		
	}
}